package com.example.demo.DAO;


import com.example.demo.pojo.Department;
import com.example.demo.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;


//部门Dao
@Repository
public class EmployeeDao {


    //模拟数据库
    private static Map<Integer, Employee> employees = null;

    @Autowired
    private DepartmentDao departmentDao;

    public static final int INT = 10005;

    static{
        employees = new HashMap<Integer, Employee>();//创建一个部门表
        employees.put(10001,new Employee(10001,"崔鹏1","128882387@qq.com",1,new Department(101,"营销部")));
        employees.put(10002,new Employee(10002,"崔鹏2","228882387@qq.com",1,new Department(102,"市场部")));
        employees.put(10003,new Employee(10003,"崔鹏3","328882387@qq.com",1,new Department(103,"财务部")));
        employees.put(10004,new Employee(10004,"崔鹏4","428882387@qq.com",1,new Department(104,"产品部")));
        employees.put(10005,new Employee(10005,"崔鹏5","528882387@qq.com",1,new Department(105,"维护部")));
    }

    //主键自增
    public static Integer intId =10006;
    // 增加一个员工

    public void save(Employee employee){
        if(employee.getId() == null){
            employee.setId(intId++);
        }
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));

        employees.put(employee.getId(),employee);
    }

    //查询全部员工信息
    public Collection<Employee> getAllemployees(){

        return employees.values();
    }
    //通过id查询员工
    public Employee getEmployeebyId(Integer id){
        return employees.get(id);
    }
    //通过id删除员工
    public void delete(Integer id){
        employees.remove(id);
    }
}
